package com.luyuan.crm.user.web.converter;

import javax.annotation.Resource;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

import org.springframework.stereotype.Component;

import com.luyuan.crm.user.Party;
import com.luyuan.crm.user.service.PartyService;
import com.ruijie.util.StringUtils;

@Component("supportPartyConverter")
public class SupportPartyConverter implements Converter {

    @Resource(name = "partyService")
    private PartyService partyService;

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        if (StringUtils.isNullOrEmpty(value)) {
            return null;
        }

        Party appUser = partyService.getByUserName(value);
        return appUser;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        Party appUser = (Party) value;
        if (appUser == null) {
            return null;
        }

        return appUser.getUserName();
    }

}
